/*
 * Filename: e:\OwnProject\FlutterPro\flutter_app_01\lib\views\form\view.dart
 * Path: e:\OwnProject\FlutterPro\flutter_app_01\lib\views\form
 * Created Date: Friday, April 2nd 2021, 2:37:07 pm
 * Author: chenfayun
 * 
 * Copyright (c) 2021 Your Company
 */

import '../../public.dart';
import './model.dart';

class AccountForm extends StatelessWidget {
  final AccountItem account;
  const AccountForm(this.account, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ProviderWidget<FormViewModal>(
      model: FormViewModal(account),
      builder: (context, model, child) {
        return Scaffold(
          appBar: IAppBar(
            model.account.id != null
                ? '${AccountList.categories[account.type]?.name}’s account'
                : 'Add ${AccountList.categories[account.type]?.name} account',
            backgroundColor: Colors.white,
            backBtn: true,
            right: GestureDetector(
              onTap: () {
                model.save().then((account) {
                  RouterHelper.navigator.pop(account);
                });
              },
              child: const Center(
                child: Text(
                  'Save',
                  style: TextStyle(color: Colors.blueAccent),
                ),
              ),
            ),
          ),
          body: formBuild(context, model),
        );
      },
    );
  }

  Widget formBuild(BuildContext context, FormViewModal model) {
    return ListView(
      children: [
        const SizedBox(height: 10),
        cellBar(controller: model.nameTextEditingController, hintText: "Please Enter Account Name"),
        cellBar(controller: model.idTextEditingController, hintText: "Please Enter Account Id"),
        cellBar(
            controller: model.pwdTextEditingController, hintText: "Please Enter Account Password"),
        cellBar(
            controller: model.remarkTextEditingController, hintText: "Please Enter Account Remark"),
      ],
    );
  }

  Widget cellBar(
      {required TextEditingController controller, required String hintText}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: ScreenUtil.setWidth(15)),
      margin: const EdgeInsets.fromLTRB(15, 0, 15, 20),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          Expanded(
            flex: 1,
            child: input(controller, hintText),
          ),
          GestureDetector(
            onTap: () {
              ClipboardData data = ClipboardData(text: controller.text);
              Clipboard.setData(data);
              Toast.show('copy success');
            },
            child: const Icon(
              Icons.copy,
              color: Colors.blueAccent,
            ),
          )
        ],
      ),
    );
  }

  Widget input(TextEditingController controller, String hintText) {
    return TextField(
      controller: controller,
      keyboardAppearance: Brightness.light,
      inputFormatters: <TextInputFormatter>[
        LengthLimitingTextInputFormatter(10) //限制长度
      ],
      // 键盘回车键
      keyboardType: TextInputType.text,
      cursorWidth: 1.5,
      cursorColor: const Color.fromRGBO(51, 51, 51, 1),
      decoration: InputDecoration(
        contentPadding: const EdgeInsets.symmetric(vertical: 0),
        hintText: hintText,
        hintStyle: TextStyle(
            fontSize: ScreenUtil.setSp(12).toDouble(), color: Colors.grey[400]),
        enabledBorder: const UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.transparent),
        ),
        focusedBorder: const UnderlineInputBorder(
          borderSide: BorderSide(color: Colors.transparent),
        ),
      ),
    );
  }
}
